package com.beiben.statistics.service.impl;

import com.beiben.statistics.mapper.AbilityMapper;
import com.beiben.statistics.service.IAbilityService;
import com.beiben.statistics.vo.AbillityDataVo;
import com.beiben.statistics.vo.NameValueVo;
import com.beiben.statistics.vo.RecordSearchParamVo;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by admin on 2020/9/14.
 */
@Service
public class AbilityServiceImpl implements IAbilityService {

    @Autowired
    private AbilityMapper abilityMapper;

    /**
     * 查询所的考试成绩柱状图
     *
     * @param
     * @return 统计集合
     */
    @Override
    public List<AbillityDataVo> selectAbilityDeptSuo(RecordSearchParamVo recordSearchParamVo) {

        return abilityMapper.selectAbilityDeptSuo(recordSearchParamVo);
    }

    /**
     * 查询科室的考试成绩柱状图
     *
     * @param
     * @return 统计集合
     */
    @Override
    public List<AbillityDataVo> selectAbilityDeptShi(RecordSearchParamVo recordSearchParamVo) {
        //查询未考试人员信息（机构ID和姓名）
        List<NameValueVo> listNo = abilityMapper.selectPercentNoPeople(recordSearchParamVo);
        List<AbillityDataVo> list = abilityMapper.selectAbilityDeptShi(recordSearchParamVo);
        for(AbillityDataVo adv : list){
            List<String> noPeople = new ArrayList<String>();
            for(NameValueVo nv : listNo){
                if(adv.getId().equals(nv.getId())){
                    String name = nv.getName();
                    noPeople.add(name);
                }
            }
            adv.setNoPeople(noPeople);
        }
        return list;
    }

    /**
     * 查询人员的考试成绩柱状图
     *
     * @param
     * @return 统计集合
     */
    @Override
    public List<AbillityDataVo> selectAbilityDeptPeople(RecordSearchParamVo recordSearchParamVo) {
        return abilityMapper.selectAbilityDeptPeople(recordSearchParamVo);
    }

    /**
     * 查询所的考试成绩分布饼状图
     *
     * @param
     * @return 统计集合
     */
    @Override
    public List<NameValueVo> selectAbilityDeptSuoCompared(RecordSearchParamVo recordSearchParamVo) {
        Integer total = abilityMapper.selectAbilityDeptSuoComparedCount(recordSearchParamVo);
        List<NameValueVo> list = abilityMapper.selectAbilityDeptSuoCompared(recordSearchParamVo);
        for(NameValueVo nv:list){
            double bbi = Arith.mul(Arith.div(Double.valueOf(nv.getValue()),total,4),100);
            nv.setValue(bbi);
        }
        return list;
    }


    /**
     *  查询室的考试成绩饼状图
     *
     * @param
     * @return 统计集合
     */
    @Override
    public List<NameValueVo> selectAbilityDeptShiCompared(RecordSearchParamVo recordSearchParamVo) {
        if(null==recordSearchParamVo.getDeptId()){
            recordSearchParamVo.setDeptId(103L);
        }
        Integer total = abilityMapper.selectAbilityDeptSuoComparedCount(recordSearchParamVo);
        List<NameValueVo> list = abilityMapper.selectAbilityDeptSuoCompared(recordSearchParamVo);
        for(NameValueVo nv:list){
            double bbi = Arith.mul(Arith.div(Double.valueOf(nv.getValue()),total,4),100);
            nv.setValue(bbi);
        }
        return list;
    }


    /**
     * 查询室下的人的考试成绩饼状图
     *
     * @param
     * @return 统计集合
     */
    @Override
    public List<NameValueVo> selectAbilityDeptPeopleCompared(RecordSearchParamVo recordSearchParamVo) {
        if(null==recordSearchParamVo.getDeptId()){
            recordSearchParamVo.setDeptId(114L);
        }
        Integer total = abilityMapper.selectAbilityDeptSuoComparedCount(recordSearchParamVo);
        List<NameValueVo> list = abilityMapper.selectAbilityDeptSuoCompared(recordSearchParamVo);
        for(NameValueVo nv:list){
            double bbi = Arith.mul(Arith.div(Double.valueOf(nv.getValue()),total,4),100);
            nv.setValue(bbi);
        }
        return list;
    }

    /**
     * 查询所的考试实考与应考柱状图
     *
     * @param
     * @return 统计集合
     */
    @Override
    public List<AbillityDataVo> selectPercentDeptSuo(RecordSearchParamVo recordSearchParamVo) {
        List<AbillityDataVo> list = abilityMapper.selectAbilityDeptSuo(recordSearchParamVo);
        for(AbillityDataVo abv: list){
            if(StringUtils.isNotEmpty(abv.getAllCount())&&"0"!=abv.getAllCount()){
                abv.setValue(Arith.mul(Arith.div( Double.valueOf(abv.getRelCount()),Double.valueOf(abv.getAllCount()),4),100));
            }else{
                abv.setValue(100.0);
            }
        }
        list.sort(Comparator.comparing(AbillityDataVo::getValue).reversed());
        return list;
    }

    /**
     * 查询科室的考试实考与应考柱状图
     *
     * @param
     * @return 统计集合
     */
    @Override
    public List<AbillityDataVo> selectPercentDeptShi(RecordSearchParamVo recordSearchParamVo) {
        List<AbillityDataVo> list = abilityMapper.selectAbilityDeptShi(recordSearchParamVo);
        for(AbillityDataVo abv: list){
            if(StringUtils.isNotEmpty(abv.getAllCount())&&"0"!=abv.getAllCount()){
                abv.setValue(Arith.mul(Arith.div( Double.valueOf(abv.getRelCount()),Double.valueOf(abv.getAllCount()),4),100));
            }else{
                abv.setValue(100.0);
            }
        }
        list.sort(Comparator.comparing(AbillityDataVo::getValue).reversed());
        return list;
    }

    /**
     * 查询人员的考试成绩及考试内容
     *
     * @param
     * @return 统计集合
     */
    @Override
    public List<AbillityDataVo> selectPercentDeptPeople(RecordSearchParamVo recordSearchParamVo) {
        return abilityMapper.selectPercentDeptPeople(recordSearchParamVo);
    }

}
